import { RouteRecordRaw } from "vue-router";
import { constantRoutes } from "@/router";
import { store} from "@/store";
import MenuAPI from "@/api/menu";
import { RouteVO } from "@/api/menu/model";

// 引入全部模块
// const modules = import.meta.glob("./**/*.ts", { eager: true });

const modules = import.meta.glob("../../views/**/**.vue");
const Layout = () => import("@/layout/index.vue");

/**
 * 用meta.role 来判断是否与当前用户权限匹配
 * 
 * @param roles 用户角色集合
 * @param route 当前路由
 * @returns 是否匹配
 */

const hasPermission = (roles: string[], route: RouteRecordRaw) => {
    
    if (route.meta && route.meta.roles) {
     // 角色【超级管理员】拥有所有权限，忽略校验
     if(roles.includes("ROOT")) {
        return true;
     }
     return roles.some((role) => {
        if(route.meta?.roles) {
            return route.meta.roles.includes(role);
        }
     });
    }
    return false;
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * 
 * @param routes 异步路由表,接口返回所有动态路由
 * @param roles 用户角色集合
 * @returns 符合用户角色权限的路由表
 */

const filterAsyncRoutes = (routes: RouteVO[], roles: string[]) => {
    const asyncRoutes: RouteRecordRaw[] = [];
    routes.forEach((route) => {
        const tmpRoute = { ...route } as RouteRecordRaw;  // 深拷贝 route 对象 避免污染
        if (hasPermission(roles, tmpRoute)) {
            // 如果是顶级目录，替换为Layout 组件
            if (tmpRoute.component?.toString() == 'Layout') {
                tmpRoute.component = Layout;
            } else {
                // 如果是子目录，动态加载组件
                const component = modules[`../../views/${tmpRoute.component}`];
                if (component) {
                    tmpRoute.component = component;
                } else {
                    tmpRoute.component = modules[`../../views/error-page/404.vue`];
                }
            }
            if (tmpRoute.children) {
                tmpRoute.children = filterAsyncRoutes(route.children, roles);
            }

            asyncRoutes.push(tmpRoute);
        }
    });
    return asyncRoutes
};

// setup store
export const usePermissionStore = defineStore("permission",()=> {
    // state
    const routes = ref<RouteRecordRaw[]>([]);

    // actions
    function setRoutes(newRoutes: RouteRecordRaw[]) {
        routes.value = constantRoutes.concat(newRoutes);
    }

    /**
     * 生成动态路由
     * 
     * @param roles 用户角色集合
     * @returns
     */
    function generateRoutes(roles: string[]) {
        return new Promise((resolve, reject) => {
            // 接口获取所有路由
            MenuAPI.getRoutes().then((data) => {
                // 过滤有权限的动态路由
                const accessRoutes = filterAsyncRoutes(data, roles);
                setRoutes(accessRoutes);
                resolve(accessRoutes);
            })
            .catch((error) => {
                reject(error)
            })
        })
    }


    /**
     * 获取与激活的顶部菜单项相关的混合模式左侧菜单集合
     */
    const mixLeftMenus = ref<RouteRecordRaw[]>([]);
    function setMixLeftMenus(topMenuPath: string) {
        const matchedItem = routes.value.find((item) => item.path === topMenuPath);
        if (matchedItem?.children) {
            mixLeftMenus.value = matchedItem.children;
        }
    }

    return {
        routes,
        setRoutes,
        generateRoutes,
        mixLeftMenus,
        setMixLeftMenus};
});

// 非setup
export function usePermissionStoreHook() {
    return usePermissionStore(store);
}